<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>导入模块 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter02.html" />
    
    
    <link rel="prev" href="./index.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="1"
        data-chapter-title="导入模块"
        data-filepath="chapter01.md"
        data-basepath="."
        data-revision="Sun Feb 16 2020 15:12:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        常见系统模块的使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        pip的安装配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        管理第三方软件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        自定义模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        包的使用
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="python&#x4E2D;&#x7684;&#x6A21;&#x5757;">Python&#x4E2D;&#x7684;&#x6A21;&#x5757;</h1>
<p>&#x5728;Python&#x4E2D;&#x6709;&#x4E00;&#x4E2A;&#x6982;&#x5FF5;&#x53EB;&#x505A;&#x6A21;&#x5757;&#xFF08;module&#xFF09;&#x3002;</p>
<p>&#x8BF4;&#x7684;&#x901A;&#x4FD7;&#x70B9;&#xFF1A;&#x6A21;&#x5757;&#x5C31;&#x597D;&#x6BD4;&#x662F;&#x5DE5;&#x5177;&#x5305;&#xFF0C;&#x8981;&#x60F3;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5DE5;&#x5177;&#x5305;&#x4E2D;&#x7684;&#x5DE5;&#x5177;(&#x5C31;&#x597D;&#x6BD4;&#x51FD;&#x6570;)&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x5BFC;&#x5165;&#x8FD9;&#x4E2A;&#x6A21;&#x5757;</p>
<p>&#x6BD4;&#x5982;&#x6211;&#x4EEC;&#x7ECF;&#x5E38;&#x4F7F;&#x7528;&#x5DE5;&#x5177; random&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x3002;&#x4F7F;&#x7528; import random &#x5BFC;&#x5165;&#x5DE5;&#x5177;&#x4E4B;&#x540E;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; random &#x7684;&#x51FD;&#x6570;&#x3002;</p>
<h2 id="&#x5BFC;&#x5165;&#x6A21;&#x5757;">&#x5BFC;&#x5165;&#x6A21;&#x5757;</h2>
<h3 id="&#x5BFC;&#x5165;&#x6A21;&#x5757;&#x6709;&#x4E94;&#x79CD;&#x65B9;&#x5F0F;"><1> &#x5BFC;&#x5165;&#x6A21;&#x5757;&#x6709;&#x4E94;&#x79CD;&#x65B9;&#x5F0F;</1></h3>
<ul>
<li>import &#x6A21;&#x5757;&#x540D;</li>
<li>from &#x6A21;&#x5757;&#x540D; import &#x529F;&#x80FD;&#x540D;</li>
<li>from &#x6A21;&#x5757;&#x540D; import *</li>
<li>import &#x6A21;&#x5757;&#x540D; as &#x522B;&#x540D;</li>
<li>from &#x6A21;&#x5757;&#x540D; import &#x529F;&#x80FD;&#x540D; as &#x522B;&#x540D;</li>
</ul>
<p>&#x4E0B;&#x9762;&#x6765;&#x6328;&#x4E2A;&#x7684;&#x770B;&#x4E00;&#x4E0B;&#x3002;</p>
<h3 id="import"><2>import</2></h3>
<p>&#x5728;Python&#x4E2D;&#x7528;&#x5173;&#x952E;&#x5B57;<code>import</code>&#x6765;&#x5F15;&#x5165;&#x67D0;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x6BD4;&#x5982;&#x8981;&#x5F15;&#x5165;&#x7CFB;&#x7EDF;&#x6A21;&#x5757; math&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5728;&#x6587;&#x4EF6;&#x6700;&#x5F00;&#x59CB;&#x7684;&#x5730;&#x65B9;&#x7528;<code>import math</code>&#x6765;&#x5F15;&#x5165;&#x3002;</p>
<p>&#x8BED;&#x6CD5;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> &#x6A21;&#x5757;<span class="hljs-number">1</span>,&#x6A21;&#x5757;<span class="hljs-number">2</span>,...  <span class="hljs-comment"># &#x5BFC;&#x5165;&#x65B9;&#x5F0F;</span>

&#x6A21;&#x5757;&#x540D;.&#x51FD;&#x6570;&#x540D;()  <span class="hljs-comment"># &#x4F7F;&#x7528;&#x6A21;&#x5757;&#x91CC;&#x7684;&#x51FD;&#x6570;</span>
</code></pre>
<ul>
<li><p>&#x60F3;&#x4E00;&#x60F3;:</p>
<blockquote>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x5FC5;&#x987B;&#x52A0;&#x4E0A;&#x6A21;&#x5757;&#x540D;&#x8C03;&#x7528;&#x5462;&#xFF1F;</p>
</blockquote>
</li>
<li><p>&#x7B54;:</p>
<blockquote>
<p>&#x56E0;&#x4E3A;&#x53EF;&#x80FD;&#x5B58;&#x5728;&#x8FD9;&#x6837;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#xFF1A;&#x5728;&#x591A;&#x4E2A;&#x6A21;&#x5757;&#x4E2D;&#x542B;&#x6709;&#x76F8;&#x540C;&#x540D;&#x79F0;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x6B64;&#x65F6;&#x5982;&#x679C;&#x53EA;&#x662F;&#x901A;&#x8FC7;&#x51FD;&#x6570;&#x540D;&#x6765;&#x8C03;&#x7528;&#xFF0C;&#x89E3;&#x91CA;&#x5668;&#x65E0;&#x6CD5;&#x77E5;&#x9053;&#x5230;&#x5E95;&#x8981;&#x8C03;&#x7528;&#x54EA;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x6240;&#x4EE5;&#x5982;&#x679C;&#x50CF;&#x4E0A;&#x8FF0;&#x8FD9;&#x6837;&#x5F15;&#x5165;&#x6A21;&#x5757;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x5FC5;&#x987B;&#x52A0;&#x4E0A;&#x6A21;&#x5757;&#x540D;</p>
</blockquote>
</li>
</ul>
<p>&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> math

<span class="hljs-comment">#&#x8FD9;&#x6837;&#x624D;&#x80FD;&#x6B63;&#x786E;&#x8F93;&#x51FA;&#x7ED3;&#x679C;</span>
<span class="hljs-keyword">print</span> math.sqrt(<span class="hljs-number">2</span>)

<span class="hljs-comment">#&#x8FD9;&#x6837;&#x4F1A;&#x62A5;&#x9519;</span>
print(sqrt(<span class="hljs-number">2</span>))
</code></pre>
<h3 id="fromimport"><3>from&#x2026;import</3></h3>
<p>&#x6709;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x7528;&#x5230;&#x6A21;&#x5757;&#x4E2D;&#x7684;&#x67D0;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x5F15;&#x5165;&#x8BE5;&#x51FD;&#x6570;&#x5373;&#x53EF;&#xFF0C;&#x6B64;&#x65F6;&#x53EF;&#x4EE5;&#x7528;&#x4E0B;&#x9762;&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> &#x6A21;&#x5757;&#x540D; <span class="hljs-keyword">import</span> &#x51FD;&#x6570;&#x540D;<span class="hljs-number">1</span>,&#x51FD;&#x6570;&#x540D;<span class="hljs-number">2.</span>...
</code></pre>
<p>&#x4E0D;&#x4EC5;&#x53EF;&#x4EE5;&#x5F15;&#x5165;&#x51FD;&#x6570;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x5F15;&#x5165;&#x4E00;&#x4E9B;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x3001;&#x7C7B;&#x7B49;</p>
<ul>
<li><p>&#x6CE8;&#x610F;:</p>
<blockquote>
<p>&#x901A;&#x8FC7;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x5F15;&#x5165;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#x53EA;&#x80FD;&#x7ED9;&#x51FA;&#x51FD;&#x6570;&#x540D;&#xFF0C;&#x4E0D;&#x80FD;&#x7ED9;&#x51FA;&#x6A21;&#x5757;&#x540D;&#xFF0C;&#x4F46;&#x662F;&#x5F53;&#x4E24;&#x4E2A;&#x6A21;&#x5757;&#x4E2D;&#x542B;&#x6709;&#x76F8;&#x540C;&#x540D;&#x79F0;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x540E;&#x9762;&#x4E00;&#x6B21;&#x5F15;&#x5165;&#x4F1A;&#x8986;&#x76D6;&#x524D;&#x4E00;&#x6B21;&#x5F15;&#x5165;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x5047;&#x5982;&#x6A21;&#x5757;A&#x4E2D;&#x6709;&#x51FD;&#x6570;function( )&#xFF0C;&#x5728;&#x6A21;&#x5757;B&#x4E2D;&#x4E5F;&#x6709;&#x51FD;&#x6570;function( )&#xFF0C;&#x5982;&#x679C;&#x5F15;&#x5165;A&#x4E2D;&#x7684;function&#x5728;&#x5148;&#x3001;B&#x4E2D;&#x7684;function&#x5728;&#x540E;&#xFF0C;&#x90A3;&#x4E48;&#x5F53;&#x8C03;&#x7528;function&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x662F;&#x53BB;&#x6267;&#x884C;&#x6A21;&#x5757;B&#x4E2D;&#x7684;function&#x51FD;&#x6570;&#x3002;</p>
</blockquote>
</li>
</ul>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x8981;&#x5BFC;&#x5165;&#x6A21;&#x5757;fib&#x7684;fibonacci&#x51FD;&#x6570;&#xFF0C;&#x4F7F;&#x7528;&#x5982;&#x4E0B;&#x8BED;&#x53E5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> fib <span class="hljs-keyword">import</span> fibonacci
</code></pre>
<h3 id="&#x6CE8;&#x610F;">&#x6CE8;&#x610F;</h3>
<ul>
<li>&#x4E0D;&#x4F1A;&#x628A;&#x6574;&#x4E2A;fib&#x6A21;&#x5757;&#x5BFC;&#x5165;&#x5230;&#x5F53;&#x524D;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E2D;&#xFF0C;&#x5B83;&#x53EA;&#x4F1A;&#x5C06;fib&#x91CC;&#x7684;fibonacci&#x5355;&#x4E2A;&#x51FD;&#x6570;&#x5F15;&#x5165;</li>
</ul>
<h3 id="from--import-"><4>from &#x2026; import *</4></h3>
<p>&#x628A;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x7684;&#x6240;&#x6709;&#x5185;&#x5BB9;&#x5168;&#x90FD;&#x5BFC;&#x5165;&#x5230;&#x5F53;&#x524D;&#x7684;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E5F;&#x662F;&#x53EF;&#x884C;&#x7684;&#xFF0C;&#x53EA;&#x9700;&#x4F7F;&#x7528;&#x5982;&#x4E0B;&#x58F0;&#x660E;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> modname <span class="hljs-keyword">import</span> *
</code></pre>
<h3 id="&#x6CE8;&#x610F;">&#x6CE8;&#x610F;</h3>
<ul>
<li>&#x8FD9;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x5BFC;&#x5165;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x9879;&#x76EE;&#x3002;&#x7136;&#x800C;&#x8FD9;&#x79CD;&#x58F0;&#x660E;&#x4E0D;&#x8BE5;&#x88AB;&#x8FC7;&#x591A;&#x5730;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<h3 id="as-&#x522B;&#x540D;"><5> as &#x522B;&#x540D;</5></h3>
<pre><code class="lang-python">In [1]: import time as tt  # &#x5BFC;&#x5165;&#x6A21;&#x5757;&#x65F6;&#x8BBE;&#x7F6E;&#x522B;&#x540D;&#x4E3A; tt

In [2]: time.sleep(1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
&lt;ipython-input-2-07a34f5b1e42&gt; in &lt;module&gt;()
----&gt; 1 time.sleep(1)

NameError: name &apos;time&apos; is not defined

In [3]: 

In [3]: tt.sleep(1)  # &#x4F7F;&#x7528;&#x522B;&#x540D;&#x624D;&#x80FD;&#x8C03;&#x7528;&#x65B9;&#x6CD5;

In [4]: 

In [4]: from time import sleep as sp  # &#x5BFC;&#x5165;&#x65B9;&#x6CD5;&#x65F6;&#x8BBE;&#x7F6E;&#x522B;&#x540D;

In [5]: sleep(1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
&lt;ipython-input-5-82e5c2913b44&gt; in &lt;module&gt;()
----&gt; 1 sleep(1)

NameError: name &apos;sleep&apos; is not defined

In [6]: 

In [6]: sp(1)  # &#x4F7F;&#x7528;&#x522B;&#x540D;&#x624D;&#x80FD;&#x8C03;&#x7528;&#x65B9;&#x6CD5;

In [7]:
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./index.html" class="navigation navigation-prev " aria-label="Previous page: 学习目标"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter02.html" class="navigation navigation-next " aria-label="Next page: 常见系统模块的使用"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
